package echat.future.cn.echat.message.rank;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.message.rank.adapter.RankAdapter;
import echat.future.cn.echat.message.rank.bean.RankBean;
import echat.future.cn.echat.my.weight.CircleImageView;
import lumingweihua.future.cn.baselibgxh.base.BaseFragment;

/**
 * Created by JX on 2018/8/19.
 */

public class RankItemFragment extends BaseFragment {

    private RelativeLayout rl1;
    private RelativeLayout rl2;
    private RelativeLayout rl3;
    private CircleImageView iv_head_diyi;
    private CircleImageView iv_head_dier;
    private CircleImageView iv_head_disan;
    private TextView tv_name1;
    private TextView tv_name2;
    private TextView tv_name3;
    private TextView tv_num1;
    private TextView tv_num2;
    private TextView tv_num3;
    private ImageView iv_icon1;
    private ImageView iv_icon2;
    private ImageView iv_icon3;
    private FrameLayout fl_jibie1;
    private FrameLayout fl_jibie2;
    private FrameLayout fl_jibie3;
    private TextView tv_jibie1;
    private TextView tv_jibie2;
    private TextView tv_jibie3;
    private ImageView iv_rz1;
    private ImageView iv_rz2;
    private ImageView iv_rz3;

    private RecyclerView rvRank;
    private RankAdapter mAdapter;

    private RankBean rankBean1;
    private RankBean rankBean2;
    private RankBean rankBean3;
    private int type;

    public static RankItemFragment newInstance(int type) {
        RankItemFragment fragment = new RankItemFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.rank_item_fragment, container, false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();

        type = getArguments().getInt("type");

        rvRank = (RecyclerView) findView(R.id.rank_rv);
        rvRank.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new RankAdapter(R.layout.rank_rv_item, new ArrayList<>(), type);
        mAdapter.bindToRecyclerView(rvRank);
    }

    private void initView() {
        rl1 = (RelativeLayout) findView(R.id.rl_1);
        rl2 = (RelativeLayout) findView(R.id.rl_2);
        rl3 = (RelativeLayout) findView(R.id.rl_3);
        iv_head_diyi = (CircleImageView) findView(R.id.iv_head_diyi);
        iv_head_dier = (CircleImageView) findView(R.id.iv_head_dier);
        iv_head_disan = (CircleImageView) findView(R.id.iv_head_disan);
        tv_name1 = (TextView) findView(R.id.tv_name1);
        tv_name2 = (TextView) findView(R.id.tv_name2);
        tv_name3 = (TextView) findView(R.id.tv_name3);
        tv_num1 = (TextView) findView(R.id.tv_num1);
        tv_num2 = (TextView) findView(R.id.tv_num2);
        tv_num3 = (TextView) findView(R.id.tv_num3);
        iv_icon1 = (ImageView) findView(R.id.iv_icon1);
        iv_icon2 = (ImageView) findView(R.id.iv_icon2);
        iv_icon3 = (ImageView) findView(R.id.iv_icon3);
        fl_jibie1 = (FrameLayout) findView(R.id.fl_jibie1);
        fl_jibie2 = (FrameLayout) findView(R.id.fl_jibie2);
        fl_jibie3 = (FrameLayout) findView(R.id.fl_jibie3);
        tv_jibie1 = (TextView) findView(R.id.tv_jibie1);
        tv_jibie2 = (TextView) findView(R.id.tv_jibie2);
        tv_jibie3 = (TextView) findView(R.id.tv_jibie3);
        iv_rz1 = (ImageView) findView(R.id.iv_rz1);
        iv_rz2 = (ImageView) findView(R.id.iv_rz2);
        iv_rz3 = (ImageView) findView(R.id.iv_rz3);
    }

    public void setData(List<RankBean> data) {
        if (mAdapter == null) return;
//        data.addAll(data);
//        data.addAll(data);
        List<RankBean> list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                rankBean1 = data.get(i);
            } else if (i == 1) {
                rankBean2 = data.get(i);
            } else if (i == 2) {
                rankBean3 = data.get(i);
            } else {
                list.add(data.get(i));
            }
        }
        mAdapter.setNewData(list);

        initThree(rankBean1, rl1, iv_head_diyi, tv_num1, tv_name1, tv_jibie1, iv_icon1, fl_jibie1, iv_rz1);
        initThree(rankBean2, rl2, iv_head_dier, tv_num2, tv_name2, tv_jibie2, iv_icon2, fl_jibie2, iv_rz1);
        initThree(rankBean3, rl3, iv_head_disan, tv_num3, tv_name3, tv_jibie3, iv_icon3, fl_jibie3, iv_rz1);

    }

    private void initThree(RankBean bean, RelativeLayout rl, ImageView iv_head, TextView tv_num, TextView tv_name, TextView tv_jibie, ImageView iv_icon, FrameLayout fl_jibie, ImageView iv_rz) {
        if (bean == null) {
            rl.setVisibility(View.INVISIBLE);
        } else {
            RequestOptions options = new RequestOptions();
            options.centerCrop()
                    .placeholder(R.mipmap.ic_launcher);
            Glide.with(getActivity()).load(bean.getUser_avatar()).apply(options).into(iv_head);
            tv_num.setText(type == 1 ? "收获" + bean.getNum() + "个" : bean.getNum() + "个");
            tv_name.setText(bean.getUsername());
            iv_icon.setImageResource(type == 1 ? R.drawable.zhuanshi : R.drawable.ph_jinbi);
            fl_jibie.setVisibility(type == 1 ? View.GONE : View.VISIBLE);
            tv_jibie.setText("LV." + bean.getLevel());
            iv_rz.setVisibility(bean.getUser_role() == 2 ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    protected void processClick(View v) {

    }
}
